package pack1;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.swim.entity.Collaboration;
import com.swim.entity.RegisteredUser;
import com.swim.entity.Skill;
import com.swim.session.CollaborationRemote;
import com.swim.session.FriendshipRemote;
import com.swim.session.RegisteredUserRemote;
import com.swim.session.SkillRemote;

public class UserProfile extends HttpServlet {

	private static final long serialVersionUID = 394843956578225480L;

	public UserProfile() {
        super();
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		HttpSession session = request.getSession();
    	if(session.getAttribute("authenticated") == null || !(boolean)session.getAttribute("authenticated")) {
    		response.sendRedirect("index.jsp");
			return;
    	}
    	showProfile(request, response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		HttpSession session = request.getSession();
    	if(session.getAttribute("authenticated") == null || !(boolean)session.getAttribute("authenticated")) {
    		response.sendRedirect("index.jsp");
			return;
    	}
    	showProfile(request, response);
	}

	private void showProfile(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		response.setContentType("text/html");

		//request.getParameterNames
		String email = request.getParameter("email");
		try {
			InitialContext jndiContext = (InitialContext) Util.getInitialContext();
			Object ref = jndiContext.lookup("RegisteredUserBeanJNDI");
			RegisteredUserRemote r = (RegisteredUserRemote) ref;

			//r.initDb();
			RegisteredUser user = r.getUserWithEmail(email);
			Object ref2 = jndiContext.lookup("FriendshipBeanJNDI");
			FriendshipRemote f = (FriendshipRemote) ref2;
			String authEmail = (String)request.getSession().getAttribute("authenticated-email");
			
			request.setAttribute("canReqFriendship", f.canRequestFriendship(authEmail,email));
			request.setAttribute("user", user);
			List<RegisteredUser> friends = r.getFriendsOf(email);
			request.setAttribute("friends", friends);
			
			Object ref3 = jndiContext.lookup("SkillBeanJNDI");
			SkillRemote sr = (SkillRemote) ref3;
			List<Skill> s = sr.getSkillByEmail(email);
			
			if(s == null)
				s = new ArrayList<Skill>();
			
			request.setAttribute("userSkills", s);
			String direct = request.getParameter("direct");
			if(direct == null)
				direct = "true";
			request.setAttribute("direct", direct);
			
			List<Collaboration> collaborationsOffered = getOfferedCollaborations(email);
			request.setAttribute("collaborationsOffered", collaborationsOffered);
			RequestDispatcher dispatcher = request.getRequestDispatcher("/WEB-INF/auth/profile.jsp");
			dispatcher.forward(request, response);
			
		} catch (NamingException e) {
			e.printStackTrace();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	private List<Collaboration> getOfferedCollaborations(String email) {
		try {
			
			InitialContext jndiContext = (InitialContext) Util.getInitialContext();
			Object ref = jndiContext.lookup("CollaborationBeanJNDI");
			CollaborationRemote r = (CollaborationRemote) ref;
			
			List<Collaboration> results = new ArrayList<Collaboration>();
			results.addAll(r.getOfferedCollaborations(email));
			return results;
		} catch (NamingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return null;
	}
}
